<?php
require('../../../global.php');
checkAdminAuthorizeAndJump('questionBank.edit');
if(isset($_GET['id'])){
    $examination = $db->get('tb_question_bank', '*', array('tbid' => $_GET['id']));
    $answer= $db->get('tb_answer', '*', array('examination_id' => $examination['tbid']));
}
?>
<!DOCTYPE html>
<html>
<head>
    <?php require('admin/_head.php'); ?>
    <title>试题管理 - <?php echo getconfig('title'); ?>管理后台</title>
</head>
<body class="gray-bg">
    <div class="wrapper wrapper-content animated fadeInRight">
        <div class="ibox float-e-margins">
            <div class="ibox-title">
                <h3>
                    <button type="button" class="btn btn-white btn-sm closelayer">返回</button>
                    编辑测试试题
                </h3>
            </div>
            <div class="ibox-content">
                <div class="row row-lg">
                    <div class="col-sm-12">
                        <form class="form-horizontal" id="editForm" action="ajax.php">
                            <input type="hidden" name="ac" value="listeditt">
                            <input type="hidden" name="id" value="<?php echo $_GET['id']; ?>">
                           <input type="hidden" name="aid" value="<?php echo $answer['tbid']; ?>">
                            <div class="form-group">
                                <label class="col-sm-2 control-label">题目</label>
                                <div class="col-sm-10">
                                    <input type="text" name="name" id="name" placeholder="请输入题目" class="form-control" value="<?php echo $examination['title']; ?>" dataType="*" nullmsg="请输入题目" errormsg="请输入题目">
                                    <span class="help-block m-b-none"></span>
                                </div>
                            </div>
                            
                            <div class="hr-line-dashed"></div>
                            <?php
                                if($examination['type']==1){
                            ?>
                            <div class="sc_panduanti">
                                <div class="form-group">
                                    <label class="col-sm-2 control-label">类型</label>
                                    <div class="col-sm-10">
                                        <select class="form-control" name="type" id="type" style="width:150px">
                                            <option value ="1">是非题</option>
                                        </select>
                                        <span class="help-block m-b-none"></span>
                                    </div>
                                </div>
                                <div class="hr-line-dashed"></div>
                                <div class="form-group shifeiti">
                                    <label class="col-sm-2 control-label">答案</label>
                                    <div class="col-sm-10">
                                        <select class="form-control" name="answersf" id="answersf" style="width:150px">
                                            <option value ="1" <?php if($answer['answer']==1){echo 'selected=""';}?>>对</option>
                                            <option value ="2" <?php if($answer['answer']==2){echo 'selected=""';}?>>错</option>
                                        </select>
                                        <span class="help-block m-b-none"></span>
                                    </div>
                                </div>

                                <div class="form-group sftzqda" <?php if($answer['answer']==2){?>style="display:block"<?php }else{?>style="display:none"<?php }?>>
                                    <label class="col-sm-2 control-label">正确答案</label>
                                    <div class="col-sm-10">
                                        <input type="text" name="answerinfo" id="answerinfo" placeholder="请输入正确答案" class="form-control" value="<?php echo $answer['answerinfo'];?>" dataType="*" nullmsg="请输入正确答案" errormsg="请输入正确答案">
                                        <span class="help-block m-b-none"></span>
                                    </div>
                                </div>
                                <div class="hr-line-dashed"></div>
                            </div>
                            <?php
                                }elseif($examination['type']==2){
                            ?>
                            <div class="sc_panduanti">
                                <div class="form-group">
                                    <label class="col-sm-2 control-label">类型</label>
                                    <div class="col-sm-10">
                                        <select class="form-control" name="type" id="type" style="width:150px">
                                            <option value ="2">单选题</option>
                                        </select>
                                        <span class="help-block m-b-none"></span>
                                    </div>
                                </div>
                                <div class="hr-line-dashed"></div>
                                <div class="form-group ">
                                    <label class="col-sm-2 control-label">选项</label>
                                    <div class="col-sm-10">
                                        <div id="gameitemBox" style="margin-bottom:10px;">
                                            <?php
                                                $choosce=explode("{|*|}",$examination['choose']);
                                                foreach($choosce as $key=>$val){
                                                    if($key!=0){
                                                        $kaoshiitem="kaoshiitem";
                                                    }else{
                                                        $kaoshiitem="";
                                                    }
                                                    $xuanxiang=explode("{*}",$val);
                                            ?>
                                            <div class="row <?php echo $kaoshiitem;?>" style="margin-bottom:10px;">
                                                <div class="col-md-3 col-sm-6" style="width:160px">
                                                    <div class="input-group">
                                                        <select class="form-control" name="answertype[]" style="width:150px" dataType="*" nullmsg="请选择选项" errormsg="请选择选项">
                                                            <option value ="">选择选项</option>
                                                            <option value ="A"<?php if($xuanxiang[0]=="A"){?>selected=""<?php }?>>A</option>
                                                            <option value ="B"<?php if($xuanxiang[0]=="B"){?>selected=""<?php }?>>B</option>
                                                            <option value ="C"<?php if($xuanxiang[0]=="C"){?>selected=""<?php }?>>C</option>
                                                            <option value ="D"<?php if($xuanxiang[0]=="D"){?>selected=""<?php }?>>D</option>
                                                            <option value ="E"<?php if($xuanxiang[0]=="E"){?>selected=""<?php }?>>E</option>
                                                        </select>
                                                        <span class="help-block m-b-none"></span>
                                                    </div>
                                                </div>
                                                <div class="col-md-3 col-sm-6" style="width:500px">
                                                    <div class="input-group">
                                                        <input type="text" name="answerqita[]" placeholder="请输入选项内容" class="form-control" value="<?php echo $xuanxiang[1];?>" dataType="*" nullmsg="请输入选项内容" errormsg="请输入选项内容" style="width:500px">
                                                        <span class="help-block m-b-none"></span>
                                                    </div>
                                                </div>
                                                <?php if($key!=0){?>
                                                <div class="col-md-3 col-sm-6" style="width:80px;margin-top: 2px;margin-left: 10px;">
                                                    <button type="button" class="btn btn-danger btn-sm btn-block delete" style="margin-bottom:0;">删除</button>
                                                </div>
                                                <?php }?>
                                            </div>
                                            <?php }?>
                                        </div>
                                        <button type="button" class="btn btn-primary btn-block" id="gameitemAdd">添加选项</button>
                                    </div>
                                </div>
                                <div class="hr-line-dashed"></div>
                                <div class="form-group">
                                    <label class="col-sm-2 control-label">正确答案</label>
                                    <div class="col-sm-10">
                                        <label class="radio-inline i-checks"><input type="radio" name="answerdui1" value="A" <?php if($answer['answer']=="A"){?>checked=""<?php }?>>A</label>
                                        <label class="radio-inline i-checks"><input type="radio" name="answerdui1" value="B" <?php if($answer['answer']=="B"){?>checked=""<?php }?>>B</label>
                                        <label class="radio-inline i-checks"><input type="radio" name="answerdui1" value="C" <?php if($answer['answer']=="C"){?>checked=""<?php }?>>C</label>
                                        <label class="radio-inline i-checks"><input type="radio" name="answerdui1" value="D" <?php if($answer['answer']=="D"){?>checked=""<?php }?>>D</label>
                                        <label class="radio-inline i-checks"><input type="radio" name="answerdui1" value="E" <?php if($answer['answer']=="E"){?>checked=""<?php }?>>E</label>
                                    </div>
                                </div>
                                <div class="hr-line-dashed"></div>
                            </div>
                            <?php
                                }else{
                            ?>
                            <div class="sc_panduanti">
                                <div class="form-group">
                                    <label class="col-sm-2 control-label">类型</label>
                                    <div class="col-sm-10">
                                        <select class="form-control" name="type" id="type" style="width:150px">
                                            <option value ="3">多选题</option>
                                        </select>
                                        <span class="help-block m-b-none"></span>
                                    </div>
                                </div>
                                <div class="hr-line-dashed"></div>
                                <div class="form-group ">
                                    <label class="col-sm-2 control-label">选项</label>
                                    <div class="col-sm-10">
                                        <div id="gameitemBox" style="margin-bottom:10px;">
                                            <?php
                                                $choosce=explode("{|*|}",$examination['choose']);
                                                foreach($choosce as $key=>$val){
                                                    if($key!=0){
                                                        $kaoshiitem="kaoshiitem";
                                                    }else{
                                                        $kaoshiitem="";
                                                    }
                                                    $xuanxiang=explode("{*}",$val);
                                            ?>
                                            <div class="row <?php echo $kaoshiitem;?>" style="margin-bottom:10px;">
                                                <div class="col-md-3 col-sm-6" style="width:160px">
                                                    <div class="input-group">
                                                        <select class="form-control" name="answertype[]" style="width:150px" dataType="*" nullmsg="请选择选项" errormsg="请选择选项">
                                                            <option value ="">选择选项</option>
                                                            <option value ="A"<?php if($xuanxiang[0]=="A"){?>selected=""<?php }?>>A</option>
                                                            <option value ="B"<?php if($xuanxiang[0]=="B"){?>selected=""<?php }?>>B</option>
                                                            <option value ="C"<?php if($xuanxiang[0]=="C"){?>selected=""<?php }?>>C</option>
                                                            <option value ="D"<?php if($xuanxiang[0]=="D"){?>selected=""<?php }?>>D</option>
                                                            <option value ="E"<?php if($xuanxiang[0]=="E"){?>selected=""<?php }?>>E</option>
                                                        </select>
                                                        <span class="help-block m-b-none"></span>
                                                    </div>
                                                </div>
                                                <div class="col-md-3 col-sm-6" style="width:500px">
                                                    <div class="input-group">
                                                        <input type="text" name="answerqita[]" placeholder="请输入选项内容" class="form-control" value="<?php echo $xuanxiang[1];?>" dataType="*" nullmsg="请输入选项内容" errormsg="请输入选项内容" style="width:500px">
                                                        <span class="help-block m-b-none"></span>
                                                    </div>
                                                </div>
                                                <?php if($key!=0){?>
                                                <div class="col-md-3 col-sm-6" style="width:80px;margin-top: 2px;margin-left: 10px;">
                                                    <button type="button" class="btn btn-danger btn-sm btn-block delete" style="margin-bottom:0;">删除</button>
                                                </div>
                                                <?php }?>
                                            </div>
                                            <?php }?>
                                        </div>
                                        <button type="button" class="btn btn-primary btn-block" id="gameitemAdd">添加选项</button>
                                    </div>
                                </div>
                                <div class="hr-line-dashed"></div>
                                <div class="form-group">
                                    <label class="col-sm-2 control-label">正确答案</label>
                                    <div class="col-sm-10">
                                        <?php
                                            $answerdan=explode(",",$answer['answer']);
                                        ?>
                                        <label class="radio-inline i-checks"><input type="checkbox" name="answerdui[]" value="A" <?php if(in_array("A",$answerdan)){?>checked=""<?php }?>>A</label>
                                        <label class="radio-inline i-checks"><input type="checkbox" name="answerdui[]" value="B" <?php if(in_array("B",$answerdan)){?>checked=""<?php }?>>B</label>
                                        <label class="radio-inline i-checks"><input type="checkbox" name="answerdui[]" value="C" <?php if(in_array("C",$answerdan)){?>checked=""<?php }?>>C</label>
                                        <label class="radio-inline i-checks"><input type="checkbox" name="answerdui[]" value="D" <?php if(in_array("D",$answerdan)){?>checked=""<?php }?>>D</label>
                                        <label class="radio-inline i-checks"><input type="checkbox" name="answerdui[]" value="E" <?php if(in_array("E",$answerdan)){?>checked=""<?php }?>>E</label>
                                    </div>
                                </div>
                                <div class="hr-line-dashed"></div>
                            </div>
                            <?php
                                }
                            ?>
                            <div class="form-group">
                                <div class="col-sm-4 col-sm-offset-2">
                                    <button class="btn btn-primary btn-lg" type="submit" id="btn-submit">保存内容</button>
                                    <button class="btn btn-white btn-lg closelayer" type="button">取消</button>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <?php require('admin/_js.php'); ?>
    <script src="<?php echo SITEROOTURL; ?>/admin/js/content.js?v=1.0.0"></script>
    <script src="<?php echo SITEROOTURL; ?>/admin/js/plugins/validform/Validform_min.js"></script>
    <script src="<?php echo SITEROOTURL; ?>/admin/js/plugins/validform/Validform_Datatype.js"></script>
    <script src="<?php echo SITEROOTURL; ?>/admin/js/plugins/webuploader/webuploader.min.js"></script>
    <script>
    $(function(){
        var closeLayer = function(){
            var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
            parent.closeLayer(index);
        }
        $('.closelayer').click(function(event){
            closeLayer();
        });

        $("#answersf").change(function(){
            if($(this).val()==2){
                $(".sftzqda").show();
            }else{
                $(".sftzqda").hide();
            }
        });

        $("body").on("change","select[name^=answertype]",function(){
            var itemn=$(this).val();
            var thisk=$(this)
            $(this).parents('.row').siblings('.row').find("select[name^=answertype]").each(function() {
                var item=$(this).val();
                if(itemn==item){
                    thisk.children("option").removeAttr("selected");
                    thisk.children("option").eq(0).attr("selected","selected");
                    swal('操作失败', '请不要选择相同的选项！', 'error');
                }else{
                    return true;
                }
            });
        });

        $("#gameitemAdd").click(function(){
            $("#gameitemBox").append(
                '<div class="row kaoshiitem" style="margin-bottom:10px;">'+
                    '<div class="col-md-3 col-sm-6" style="width:160px">'+
                        '<div class="input-group">'+
                            '<select class="form-control" name="answertype[]" style="width:150px" dataType="*" nullmsg="请选择选项" errormsg="请选择选项">'+
                                '<option value ="">选择选项</option>'+
                                '<option value ="A">A</option>'+
                                '<option value ="B">B</option>'+
                                '<option value ="C">C</option>'+
                                '<option value ="D">D</option>'+
                                '<option value ="E">E</option>'+
                            '</select>'+
                            '<span class="help-block m-b-none"></span>'+
                        '</div>'+
                    '</div>'+
                    '<div class="col-md-3 col-sm-6" style="width:500px">'+
                        '<div class="input-group">'+
                            '<input type="text" name="answerqita[]" placeholder="请输入选项内容" class="form-control" value="" dataType="*" nullmsg="请输入选项内容" errormsg="请输入选项内容" style="width:500px">'+
                            '<span class="help-block m-b-none"></span>'+
                        '</div>'+
                    '</div>'+
                    '<div class="col-md-3 col-sm-6" style="width:80px;margin-top: 2px;margin-left: 10px;">'+
                        '<button type="button" class="btn btn-danger btn-sm btn-block delete" style="margin-bottom:0;">删除</button>'+
                    '</div>'+
                '</div>'
            );
        });
        $('#gameitemBox').on('click', '.kaoshiitem .delete', function(event) {
            $(this).parents('.kaoshiitem').remove();
        });

        var form = $('#editForm').Validform({
    		btnSubmit: '#btn-submit',
    		postonce: false,
            ignoreHidden:true,
    		showAllError: true,
    		//msg：提示信息;
    		//o:{obj:*,type:*,curform:*}, obj指向的是当前验证的表单元素（或表单对象），type指示提示的状态，值为1、2、3、4， 1：正在检测/提交数据，2：通过验证，3：验证失败，4：提示ignore状态, curform为当前form对象;
    		//cssctl:内置的提示信息样式控制函数，该函数需传入两个参数：显示提示信息的对象 和 当前提示的状态（既形参o中的type）;
    		tiptype: function(msg, o){
    			if(!o.obj.is('form')){//验证表单元素时o.obj为该表单元素，全部验证通过提交表单时o.obj为该表单对象;
    				var B = o.obj.parents('.form-group');
    				var T = B.find('.help-block');
    				if(o.type == 2){
    					B.removeClass('has-error').addClass('has-success');
    					T.html('');
    				}else{
    					B.removeClass('has-success').addClass('has-error');
    					T.html("<i class='fa fa-times-circle'></i> " + msg);
    				}
    			}
    		},
    		ajaxPost: true,
            beforeSubmit:function(){
                var type=$("#type").val()

        	},
    		callback: function(data){
				if(data.status == 'y'){
                    parent.swal('操作成功', '', 'success');
                    closeLayer();
				}else{
                    swal('操作失败', '', 'error');
				}
    		}
    	});
    });
    </script>
</body>
</html>
